package com.smdp.utils;

import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * 布隆过滤器工具类。
 * 用于过滤部分参数有误（目标数据不存在）的请求。
 */
@Component
public class BloomFilterUtil {

    @Resource
    RedissonClient redissonClient;

    /**
     * 布隆过滤器
     */
    public static RBloomFilter<Object> shopBloomFilter;
    public static RBloomFilter<Object> seckillBloomFilter;
    public static RBloomFilter<Object> voucherBloomFilter;

    /**
     * 加载布隆过滤器
     */
    @PostConstruct
    public void init(){
        shopBloomFilter = redissonClient.getBloomFilter("shop-bloom-filter");
        seckillBloomFilter = redissonClient.getBloomFilter("seckill-bloom-filter");
        voucherBloomFilter = redissonClient.getBloomFilter("voucher-bloom-filter");
        //初始化，容量100000，错误率千分之三
        shopBloomFilter.tryInit(100000, 0.003);
        seckillBloomFilter.tryInit(100000, 0.003);
        voucherBloomFilter.tryInit(100000, 0.003);
    }
}
